package org.example.aicloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 创建线程池
 */
@Component
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolTaskExecutor getThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程数
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors()+1);

        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors()*2+1);
        executor.setQueueCapacity(100000);
        //执行自定义的拒绝策略
        executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                //todo 1.保持拒绝任务
                //todo 2.通知相关负责人进行手动处理
            }
        });
        executor.initialize();
        return executor;
    }
}
